package com.cabrida.livestar.star;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.mainstar.MainStarActivity;
import com.cabrida.livestar.model.FansAdvanceListData;
import com.cabrida.livestar.model.FansCircleMsgChildrenData;
import com.cabrida.livestar.model.FansCircleMsgData;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.view.CircleImageView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 粉丝端明星留言
 */
public class StarLeaveMsgActivity extends BaseActivity {
	public static final String TAG = StarLeaveMsgActivity.class.getSimpleName();
	private ImageView iv_back;
	private TextView tv_title;
	private ListView listView;
	private StarLeaveMsgLvAdapter adapter;
	private String clickMsgCid = "";
	private String clickMsgCudid = "";
	private String clickMsgCnickname = "";
	private CircleImageView iv_header;
	private EditText et_content;
	private TextView tv_send;
	
	private String suid;
	private int type;

	public static final String ACTION_LEAVE_MSG = "com.cabrida.livestar_leave_msg_children";
	private BroadcastReceiver mReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(ACTION_LEAVE_MSG)) {
				FansCircleMsgChildrenData clickmsgData = (FansCircleMsgChildrenData) intent
						.getSerializableExtra("data");
				clickMsgCid = clickmsgData.getC_parent();
				clickMsgCudid = clickmsgData.getC_udid();
				clickMsgCnickname = clickmsgData.getCnickname();
				et_content.setText("回复" + clickMsgCnickname + ":");
				et_content.setSelection(et_content.getText().length());
			}
		}
	};

	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
	};

	/** 获取留言数据 */
	private void getMsgList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("cid", suid);
		params.put("type", type);
		params.put("page", 1);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_STARCIRCLEMSG_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getMsgList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							JSONArray list = JsonUtils.getJSONArray(data,
									"list", new JSONArray());
							ArrayList<FansCircleMsgData> infoList = JSONParser
									.getFansCircleMsgList(list);
							adapter.setData(infoList);
							adapter.notifyDataSetChanged();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 发评论
	 * type10	呼唤明星留言	20	动态留言	30	明星主页留言	40	明星主页私信	
	 * parentId被回复的留言ID
	 * pudid被回复的用户ID
	 *  */
	private void sendContent(String description,String parentId,String pudid) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("cid", suid);
		params.put("type", type);
		params.put("description", description);
		params.put("parent", parentId);
		params.put("pudid", pudid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_CIRCLE_SEND_CONTENT_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "sendContent", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
							et_content.setText("");
							getMsgList();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IntentFilter filter_ad = new IntentFilter();
		filter_ad.addAction(ACTION_LEAVE_MSG);
		registerReceiver(mReceiver, filter_ad);
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_fans_leave_msg);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		listView = (ListView) findViewById(R.id.listView_msg);
		
		iv_header = (CircleImageView) findViewById(R.id.iv_header);
		MyUtils.loadImg(Global.headPic, iv_header);
		et_content = (EditText) findViewById(R.id.et_content);
		tv_send = (TextView) findViewById(R.id.tv_send);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		suid = getIntent().getStringExtra("suid");
		type = getIntent().getIntExtra("type", Global.MESSAGE_TYPE_1);
		iv_back.setOnClickListener(this);
		tv_send.setOnClickListener(this);
		tv_title.setText("留言");

		adapter = new StarLeaveMsgLvAdapter(context);
		listView.setAdapter(adapter);

		getMsgList();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.tv_send:
			String str_send = et_content.getText().toString().trim();
			if (TextUtils.isEmpty(str_send)||"".equals(str_send)) {
				showToast("请输入评论内容!");
				return;
			}
			if (!TextUtils.isEmpty(clickMsgCid)&&!TextUtils.isEmpty(clickMsgCudid)&&!TextUtils.isEmpty(clickMsgCnickname)) {
				if (str_send.equals("回复"+clickMsgCnickname+":")) {
					showToast("请输入回复内容!");
					return;
				}
				if (str_send.startsWith("回复"+clickMsgCnickname+":")) {
					String strs[] = str_send.split("回复"+clickMsgCnickname+":");
					sendContent(strs[1],clickMsgCid,clickMsgCudid);
				}else {
					sendContent(str_send,"","");
				}
			}else {
				sendContent(str_send,"","");
			}
			break;
		default:
			break;
		}
	}
}
